<template>
  <div id="msg">
    <div class="title">
      <span>试卷列表</span>
      <span>/  {{examData.data.subject}}</span>
    </div>
    <div class="wrapper">
      <ul class="top">
        <li class="example">{{examData.data.subject}}</li>
        <li class="right">
          <div>
            <span class="count">总分</span>
            <span class="score">{{examData.score[0]+examData.score[1]+examData.score[2]}}</span>
          </div>
        </li>
      </ul>
      <ul class="bottom">
        <li>更新于{{examData.data.examDate}}</li>
        <li>来自 {{examData.data.institute}}</li>
        <li>{{examData.data.type}}</li>
        <li class="right"><el-button @click="toAnswer(examData.data)">开始答题</el-button></li>
      </ul>
      <ul class="info">
        <li><a href="javascript:;" @click="dialogVisible = true"><el-icon><InfoFilled/></el-icon>考生须知</a></li>
      </ul>
    </div>
    <div class="content">
      <el-collapse>
        <el-collapse-item class="header">
          <template v-slot:title>
            <div class="title">
              <span>{{examData.data.subject}}</span><i class="header-icon el-icon-info"></i>
              <span class="time">{{examData.data.examScore}}分 / {{examData.data.time}}分钟</span>
              <el-button type="primary" size="small">点击查看试题详情</el-button>
            </div>
          </template>
          <ul class="inner">
            <li>
              <div class="titlei">选择题 (共{{examData.topicCount[0]}}题 共计{{examData.score[0]}}分)</div>
            </li>
            <li>
              <div class="titlei">填空题 (共{{examData.topicCount[1]}}题  共计{{examData.score[1]}}分)</div>
            </li>
            <li name="3">
              <div class="titlei">判断题 (共{{examData.topicCount[2]}}题 共计{{examData.score[2]}}分)</div>
            </li>
          </ul>
        </el-collapse-item>

      </el-collapse>
    </div>
    <!--考生须知对话框-->
    <el-dialog
        title="考生须知"
        v-model="dialogVisible"
        width="30%">
      <span>{{examData.data.tips === null ? '无' : examData.data.tips}}</span>
      <template v-slot:footer>
        <el-button @click="dialogVisible = false">知道了</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {useRoute, useRouter} from "vue-router/dist/vue-router";
import {getCurrentInstance, onMounted, reactive, ref} from "vue";
import {useStore} from "vuex";
import {InfoFilled} from '@element-plus/icons-vue'
import {ElMessage, ElMessageBox} from "element-plus";
export default {
  name: "examMsg",
  components: {InfoFilled},
  setup(){
    const router = useRouter()
    const route = useRoute()
    const {proxy} = getCurrentInstance()
    const store = useStore()
    let dialogVisible = ref(false)//对话框属性
    let examData = reactive({
      data: {},//考试信息
      question: {},//题目信息
      score: [],//每种类型分数的总数
      topicCount: [],//每种类型题目的总数
    })
    onMounted(()=>{
      init()
    })
    function init(){
      proxy.$axios.get(
          // eslint-disable-next-line no-undef
          `/exam/${route.query.id}`
      ).then(
          response => {
            examData.data = response.data
            proxy.$axios.get(
                // eslint-disable-next-line no-undef
                `/paper/${examData.data.paperId}`
            ).then(
                response => {
                  examData.question = response.data
                  const arr = Object.keys(examData.question) //获取key，也就是1，2，3
                  arr.forEach((item)=>{
                    let data = examData.question[item]//获取每种题型数据
                    examData.topicCount.push(data.length)
                    let currentScore = 0
                    for(let i = 0; i< data.length; i++) { //循环每种题型,计算出总分
                      currentScore += data[i].score
                    }
                    examData.score.push(currentScore) //把每种题型总分存入score
                  })
                },
                error => {
                  console.log('请求失败', error.message)
                }
            )
          },
          error => {
            console.log('请求失败', error.message)
          }
      )
    }
    function toAnswer(data){
      if (data.practiceOrExam === '考试' && store.state.PracticeOrExam === true){
        ElMessageBox.confirm(
            '当前为练习模式，此题库只支持考试模式，系统将强制进入考试模式，考试模式系统会启动反作弊，若切出页面超过次数会自动交卷，是否确定',
            '警告',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            }
        ).then(() => {
          ElMessage({
            type: 'success',
            message: '进入考试模式',
          })
          store.commit('changeMode',false)
          router.push({path:"/answer",query:{id: data.id}})
        }).catch(() => {
          ElMessage({
            type: 'info',
            message: '取消考试',
          })
          return
        })
      }else {
        router.push({path:"/answer",query:{id: data.id}})
      }
    }
    return{
      examData,
      dialogVisible,
      toAnswer
    }
  }
}
</script>

<style scoped>
.right {
  margin-left: auto;
}
.inner .contenti .question {
  margin-left: 40px;
  color: #9a9a9a;
  font-size: 14px;
}
.content .inner .titlei {
  margin-left: 20px;
  font-size: 16px;
  color: #88949b;
  font-weight: bold;
}
.content .title .time {
  font-size: 16px;
  margin-left: 420px;
  color: #999;
}
.content .stitle {
  background-color: #0195ff;
}
.content .title span {
  margin-right: 10px;
}
#msg .content .title {
  font-size: 20px;
  margin: 0px;
  display: flex;
  align-items: center;
}
.content {
  margin-top: 20px;
  background-color: #fff;
}
.content .header {
  padding: 10px 30px;
}
.wrapper .info {
  margin: 20px 0px 0px 20px;
  border-top: 1px solid #eee;
  padding: 20px 0px 10px 0px;
}
.wrapper .info a {
  color: #88949b;
  font-size: 14px;
}
.wrapper .info a:hover {
  color: #0195ff;
}
.wrapper .bottom {
  cursor: auto;
  padding: 5px 10px;
  border: 1px solid #88949b;
  border-radius: 4px;
}
.wrapper .bottom {
  display: flex;
  margin-left: 20px;
  color: #999;
  font-size: 14px;
  align-items: center;
}
.wrapper .bottom li {
  margin-right: 14px;
}
#msg {
  background-color: #eee;
  width: 980px;
  margin: 0 auto;
}
#msg .title {
  margin: 20px;
}
#msg .wrapper {
  background-color: #fff;
  padding: 10px;
}
.wrapper .top {
  display: flex;
  margin: 20px;
  align-items: center;
}
.wrapper .top .right {
  margin-left: auto;
}
.wrapper .top .example {
  color: #333;
  font-size: 22px;
  font-weight: 700;
}
.wrapper .top li i {
  margin-left: 20px;
  color: #88949b;
}
.wrapper .right .count {
  margin-right: 60px;
  color: #fff;
  padding: 4px 10px;
  background-color: #88949b;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  border: 1px solid #88949b;
}
.wrapper .right .score {
  position: absolute;
  left: 53px;
  top: -5px;
  padding: 1px 12px;
  font-size: 20px;
  color: #88949b;
  border: 1px dashed #88949b;
  border-left: none;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  font-weight: bold;
}
.wrapper .right div {
  position: relative;
}
</style>